/*
 *  为了捉取用户的行为添加这个文件，主要包括两个类，一个用于操作Cookie，可以作为通用类
 *  另外一个用来操作通用类
 *  alvayang@sohu-inc.com
 *  2009-02-28
 * */
var MailCookie = Class.create();
MailCookie.prototype = {
    initialize: function(){
    },
    setCookie: function(name, value, expires, path, domain, secure){
        // set time, it's in milliseconds
        var today = new Date();
        today.setTime(today.getTime());
        if (expires) {
            expires = expires * 1000 * 60 * 60 * 24;
        }
        var expires_date = new Date(today.getTime() + (expires));
        document.cookie = name + "=" + escape(value) +
        ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "") +
        ((secure) ? ";secure" : "");
    },
    getCookie: function(check_name){
        // first we'll split this cookie up into name/value pairs
        // note: document.cookie only returns name=value, not the other components
        var a_all_cookies = document.cookie.split(';');
        var a_temp_cookie = '';
        var cookie_name = '';
        var cookie_value = '';
        var b_cookie_found = false; // set boolean t/f default f
        for (i = 0; i < a_all_cookies.length; i++) {
            // now we'll split apart each name=value pair
            a_temp_cookie = a_all_cookies[i].split('=');
            // and trim left/right whitespace while we're at it
            cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
            // if the extracted name matches passed check_name
            if (cookie_name == check_name) {
                b_cookie_found = true;
                // we need to handle case where cookie has no value but exists (no = sign, that is):
                if (a_temp_cookie.length > 1) {
                    cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
                }
                // note that in cases where cookie is initialized but no value, null is returned
                return cookie_value;
                break;
            }
            a_temp_cookie = null;
            cookie_name = '';
        }
        if (!b_cookie_found) {
            return null;
        }
    },
    deleteCookie: function(name, path, domain){
        if (this.getCookie(name)) 
            document.cookie = name + "=" +
            ((path) ? ";path=" + path : "") +
            ((domain) ? ";domain=" + domain : "") +
            ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    },
    appendCookie: function(name, value, expires, path, domain, secure){
        if (name == null || name == undefined || value == null || value == undefined) {
            return -1;
        }
        var oldcookievalue = this.getCookie(name) == null ? "" + value : this.getCookie(name) + "" + value;
        this.setCookie(name, oldcookievalue, expires, path, domain, secure);
    }
};
var mailcookie = new MailCookie();

var JSOPLOG = Class.create();
JSOPLOG.prototype = {
    initialize: function(){
        this.uc = '';
		this.domain = localDomain;
        this.loadcookiename = 'mc_loadprogress';
        // this.staycookiename = 'mc_stay';
        this.progressurl = '/jsoplog/log?load=';
        this.stayurl = '/jsoplog/log?exit=';
        this.ucurl = '/jsoplog/log?uc=';
		this._url = this._location();
    },
	_location: function() {
		var _url = window.location.href;
		var _ua = _url.split('/');
		try {
			return _ua.slice(0, (_ua.length - 1)).join('/') + '/';
		} catch(e) {
			return _url;
		}
	},
    _getPVUrl: function(key){
        return "http://pv.sohu.com/pv.gif?t?_" + (+new Date()) + "?r?=" + this._url + key;
    },
    loadProgress: function(){
        var ds = +new Date();
        mailcookie.appendCookie(this.loadcookiename, "|" + ds, "", "/", this.domain, "");
    },
    sendLoadProgress: function(){
        var load = mailcookie.getCookie(this.loadcookiename);
        if (!load || load == null) 
            load = [RECORD_LOAD_TIME[0], '|', RECORD_LOAD_TIME[0]].join('');
        load += '|' + RECORD_LOAD_TIME.join('|');
        var _load = load.split('|');
        var new_load = [];
        for (var i = 0; i < _load.length; i++) {
            if (_load[i + 1]) 
                new_load.push(_load[i + 1] - _load[i]);
        }
        load = new_load.join('|');
        //crateUCPoint was defined in _add_filter.js
        createUCPoint().src = this.progressurl + load + "&r=" + Math.round(Math.random() * 1000000);
        mailcookie.deleteCookie(this.loadcookiename, "/", this.domain);
        
        // 这个数据先留着，调试的时候可以观察
        // RECORD_LOAD_TIME = [];
    },
    sendStay: function(){
        this.stayCapture("end");
        // var stay = mailcookie.getCookie(this.staycookiename);
        //crateUCPoint was defined in _add_filter.js
        createUCPoint().src = this.stayurl + "&uc=" + this.uc + "&r=" + Math.round(Math.random() * 1000000);
        // mailcookie.deleteCookie(this.staycookiename, "/", this.domain);
        this.uc = '';
    },
    sendPV: function(key){
        //发送PV的动作到PV收集Server
        cretePVPoint().setAttribute('src', this._getPVUrl(key));
    },
    sendUC: function(){
        if (this.uc.strip() == "") {
            return;
        }
        //crateUCPoint was defined in _add_filter.js
        createUCPoint().src = this.ucurl + this.uc + "&r=" + Math.round(Math.random() * 1000000);
        this.uc = '';
    },
    stayCapture: function(l_state){
        // var d = new Date();
        var ds = +new Date();
        mailcookie.appendCookie(l_state + "|" + ds, "", "/", this.domain, "");
        // var c = mailcookie.getCookie(this.staycookiename);
        var m = '1234';
    },
    /* oplog key:
     *mailcheck
     *mailwrite
     *mailaddress
     *mailmobile
     *mailrss
     *mailscore
     *mailcard
     *mailindex
     *mailindextop
     *mailexit
     *changeskin
     *mailconfig
     *mailuserfolder
     *mailunread
     *mailinbox
     *maildraft
     *maildraft
     *maildelete
     *mailgabage
     *mailReplyIndex
     *mailUnreadIndex
     *mailScoreIndex
     *
     *
     * */
    ucCapture: function(l_key){
        this.uc += "|" + l_key;
    }
};

var jsoplog = new JSOPLOG();
